/*
 * DemoSessionListener.java
 *
 * Research In Motion Limited proprietary and confidential
 * Copyright Research In Motion Limited, 2011-2011
 */
package com.allkapps.bbmopenchat;

import com.allkapps.bbmopenchat.BaseScreen;

import net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformData;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformIncomingJoinRequest;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformSession;
import net.rim.blackberry.api.bbm.platform.io.BBMPlatformSessionListener;
import net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact;
import net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContactList;

/**
 * This session listener delegates the duties to a {@link BBMOpenChatConnectionListener}.
 * It leverages the inheritance properties of connections and sessions to simplify
 * implementation.
 * <p>
 * A channel listener is required when creating a session e.g. in {@link BBMBridge#createSession(SessionScreen)}.
 * The listener can also be accessed using {@link BBMPlatformConnection#getListener()}.
 *
 * @see {@link BBMBridge#createSession(SessionScreen)}
 * @see {@link BBMPlatformConnection#getListener()}
 * @see {@link net.rim.blackberry.api.bbm.platform.service.MessagingService}
 */
public class BBMOpenChatSessionListener extends BBMPlatformSessionListener {

    private final BBMOpenChatConnectionListener _connectionListener;
    private BaseScreen screen = null;

    BBMOpenChatSessionListener() {
        _connectionListener = new BBMOpenChatConnectionListener();
    }
    
    public BaseScreen getScreen()
    {
    	return screen;
    }
    
    public void setScreen(BaseScreen screen)
    {
    	this.screen = screen;
    	_connectionListener.setScreen(this.screen);
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#contactDeclined(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact)
     */
    public void contactDeclined( BBMPlatformConnection connection, BBMPlatformContact contact ) {
        _connectionListener.contactDeclined( connection, contact );

    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#contactLeft(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact)
     */
    public void contactLeft( BBMPlatformConnection connection, BBMPlatformContact contact ) {
        _connectionListener.contactLeft( connection, contact );

    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#contactsInvited(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContactList)
     */
    public void contactsInvited( BBMPlatformConnection connection, BBMPlatformContactList contactList ) {
        _connectionListener.contactsInvited( connection, contactList );

    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#contactsJoined(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContactList, java.lang.String, int)
     */
    public void contactsJoined( BBMPlatformConnection connection, BBMPlatformContactList contactList, String cookie, int type ) {
    	if(screen != null){
    		screen.updateScreen(BaseScreen.CONTACT_JOINED, contactList);
    	} else {
    		//If you didnt set a screen to be updated but want to do something else here
    	}
        _connectionListener.contactsJoined( connection, contactList, cookie, type );

    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnectionListener#dataReceived(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact, net.rim.blackberry.api.bbm.platform.io.BBMPlatformData)
     */
    public void dataReceived( BBMPlatformConnection connection, BBMPlatformContact sender, BBMPlatformData data ) {
    	_connectionListener.setScreen(getScreen());
        _connectionListener.dataReceived( connection, sender, data );
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformSessionListener#broadcastDataReceived(net.rim.blackberry.api.bbm.platform.io.BBMPlatformSession, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact, net.rim.blackberry.api.bbm.platform.io.BBMPlatformData)
     */
    public void broadcastDataReceived( BBMPlatformSession connection, BBMPlatformContact sender, BBMPlatformData data ) {
        dataReceived(connection, sender, data);
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformSessionListener#contactsRemoved(net.rim.blackberry.api.bbm.platform.io.BBMPlatformSession, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContact, net.rim.blackberry.api.bbm.platform.profile.BBMPlatformContactList)
     */
    public void contactsRemoved( BBMPlatformSession session, BBMPlatformContact removedBy, BBMPlatformContactList contactList ) {
    	if(screen != null){
    		screen.updateScreen(BaseScreen.CONTACT_LEFT, removedBy, contactList);
    	} else {
	    	//If you didnt set a screen to be updated but want to do something else here
	    }
    }

    
    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformSessionListener#joinRequestReceived(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.io.BBMPlatformIncomingJoinRequest, java.lang.String)
     */
    public void joinRequestReceived( BBMPlatformConnection connection, BBMPlatformIncomingJoinRequest joinRequest, String cookie ) {
        _connectionListener.joinRequestReceived( connection, joinRequest, cookie );
    }

    /* (non-Javadoc)
     * @see net.rim.blackberry.api.bbm.platform.io.BBMPlatformSessionListener#joinRequestCanceled(net.rim.blackberry.api.bbm.platform.io.BBMPlatformConnection, net.rim.blackberry.api.bbm.platform.io.BBMPlatformIncomingJoinRequest, int)
     */
    public void joinRequestCanceled( BBMPlatformConnection connection, BBMPlatformIncomingJoinRequest joinRequest, int reasonCode ) {
        _connectionListener.joinRequestCanceled( connection, joinRequest, reasonCode );
    }

    /**
     * Call this when the session ends.
     */
    protected void sessionEnded() {
    	if(screen != null){
    		screen.updateScreen(BaseScreen.SESSION_ENDED);
    	} else {
	    	//If you didnt set a screen to be updated but want to do something else here
	    }
    }
}
